package zzs.generalframework.util.core.string;

/**
 * @title
 * @description
 * @project zzs-util
 * @package zzs.generalframework.util.core.string
 * @author zzs
 * @date 2018年1月30日
 * @version 1.0
 */
public class StringUtils
{
	/**
	 * 判断字符是否不为空且不为空字符串
	 * 
	 * @param inputStr
	 * @return
	 */
	public static boolean isNotNullAndBlank(String inputStr)
	{
		if (inputStr != null && !"".equals(inputStr.trim()))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	public static boolean isNull(Object obj)
	{
		if (obj == null)
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * 判断字段串是否为空或空字符串
	 * 
	 * @param inputStr
	 * @return
	 */
	public static boolean isNullOrBlank(String inputStr)
	{
		return !isNotNullAndBlank(inputStr);
	}

	/**
	 * 按指定长度格式化字符串
	 * 
	 * @param srcString
	 *            源字符串
	 * @param fillString
	 *            长度不足时补充的值
	 * @param length
	 *            格式化长度,如果为负,在左侧填充或从右侧开始截取,如果为正,在右侧填充或从左侧开始截取
	 * @return
	 */
	public static String formatStr(String srcString, String fillString, int length)
	{
		String returnString = null;
		int absLength = Math.abs(length);
		if (srcString == null)
		{
			srcString = "";
		}
		int srcLength = srcString.length();

		// 源字符串长度大于所需结果字符串长度,截取字符
		if (srcLength >= absLength)
		{
			int startPos = 0;
			// 所需要长度>＝0,从左侧截取
			if (length >= 0)
			{
				startPos = 0;
			}
			// 所需要长度<0,从右侧截取
			else
			{
				startPos = srcLength - absLength;
			}
			int endPos = startPos + absLength;
			returnString = srcString.substring(startPos, endPos);
		}
		// 源字符串长度小于等于所需结果字符串长度,填充字符
		else
		{
			int fillLength = absLength - srcLength;
			if (fillString == null || "".equals(fillString))
			{
				fillString = " ";
			}
			int fillStrLength = fillString.length();
			int fillCount = fillLength / fillStrLength;
			if (fillLength % fillStrLength != 0)
			{
				fillCount++;
			}
			StringBuffer fillBuffer = new StringBuffer();
			for (int i = 0; i < fillCount; i++)
			{
				fillBuffer.append(fillString);
			}
			fillString = fillBuffer.substring(0, fillLength);
			// 所需长度>=0,在右端填充
			if (length >= 0)
			{
				returnString = srcString + fillString;
			}
			// 所需长度<,在左端填充
			else
			{
				returnString = fillString + srcString;
			}
		}

		return returnString;
	}

}
